package com.winning.ds.role.service.impl;

import com.winning.ds.common.entity.SysGncdEntity;
import com.winning.ds.common.util.UUIDUtils;
import com.winning.ds.role.dao.MenuDao;
import com.winning.ds.role.entity.SysGncdGnanEntity;
import com.winning.ds.role.entity.SysGncdQxanEntity;
import com.winning.ds.role.entity.SysGncdTreeEntity;
import com.winning.ds.role.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: plat
 * @description:
 * @author: nxs
 * @create: 2018-07-20 15:25
 **/
@Service("MenuService")
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;


    @Override
    public List<SysGncdTreeEntity> queryMenuByAppid(String yydm) {

        List<SysGncdTreeEntity> menuList= menuDao.queryMenuByAppid(yydm);

        return hasChildGncd(menuList);
    }

    @Override
    public List<SysGncdTreeEntity> queryMenuByParams(Map queryParams) {

        List<SysGncdTreeEntity> menuList= menuDao.queryMenuByParams(queryParams);

        return menuList;
    }



    @Override
    public List<SysGncdTreeEntity> hasChildGncd(List<SysGncdTreeEntity> yyzcList1) {

        List<SysGncdTreeEntity> menuList=yyzcList1;
        List<SysGncdTreeEntity> yyzcDjcdList=new ArrayList<SysGncdTreeEntity>(); //顶级菜单列表

        //找出顶级菜单
        for(SysGncdTreeEntity sysGncd:menuList){
            Boolean isDjcd=true;
            for(SysGncdTreeEntity sysGncd1:menuList){
                if(sysGncd1.getId().equals(sysGncd.getpId())){
                    isDjcd=false;
                }
            }
            if(isDjcd){
                yyzcDjcdList.add(sysGncd);
            }
        }
        //为顶级菜单yyzcDjcdList中的childList中递归放入子菜单
        yyzcDjcdList = childGncd(menuList,yyzcDjcdList);
        return yyzcDjcdList;
    }

    //为顶级菜单yyzcDjcdList中的childList中递归放入子菜单
    public List<SysGncdTreeEntity> childGncd(List<SysGncdTreeEntity> menuList,List<SysGncdTreeEntity> yyzcDjcdList){

        for(SysGncdTreeEntity sysGncd:yyzcDjcdList){
           // int i=0;
            String id=sysGncd.getId();
            List<SysGncdTreeEntity> lists = new ArrayList();
            for(SysGncdTreeEntity sz:menuList){
                if(id.equals(sz.getpId())){
                   // i++;
                   // sz.setMgorder(i);
                    lists.add(sz);
                }
            }
            if(lists.size()>0){
                List<SysGncdTreeEntity> childGncd = childGncd(menuList,lists);//递归获取子菜单
                sysGncd.setChildren(childGncd);
                sysGncd.setHasChildren(true);
                sysGncd.setYzjd("0");
            }

        }

        return yyzcDjcdList;
    }

    @Override
    public void saveMenu(SysGncdEntity sge, String mcode) {

        //根据pcode查询父级菜单级别，然后确定本菜单级别
        String pcode = sge.getPcode();
        SysGncdEntity psge = menuDao.queryGncdInfo(pcode);
        //int pcdjb=0;
        if (psge.getCdjb() != null) {
            int pcdjb = psge.getCdjb();
            sge.setCdjb(pcdjb + 1);
        }

        sge.setMcode(mcode);
        menuDao.saveMenu(sge);
        menuDao.updateisYzjd(mcode);//更新修改后菜单叶子节点
        menuDao.updateisNotYzjd(mcode);
        menuDao.updateisYzjd(pcode);//更新修改后父级菜单的叶子节点
        menuDao.updateisNotYzjd(pcode);
    }

    @Override
    public void saveGnan(SysGncdGnanEntity sgge) {

        sgge.setLsh(UUIDUtils.getUUID());
        sgge.setJlzt("0");
        menuDao.saveGnan(sgge);
    }

    @Override
    public void saveQxan(SysGncdQxanEntity sgge) {

        sgge.setSjlsh(UUIDUtils.getUUID());
        sgge.setJlzt("0");
        menuDao.saveQxan(sgge);
    }

    @Override
    public List<SysGncdGnanEntity> queryGnan(String sjcddm) {
        return menuDao.queryGnan(sjcddm);
    }

    @Override
    public List<SysGncdQxanEntity> queryQxan(String sjsjcddm) {
        return menuDao.queryQxan(sjsjcddm);
    }

    @Override
    public void delGnan(String lsh) {
        menuDao.delGnan(lsh);
    }


    @Override
    public void delQxan(String sjlsh) {
        menuDao.delQxan(sjlsh);
    }

    @Override
    public SysGncdGnanEntity queryGnanBylsh(String lsh) {
        return menuDao.queryGnanBylsh(lsh);
    }

    @Override
    public List<SysGncdGnanEntity> queryGnanByParams(Map<String,Object> queryParams) {
        return menuDao.queryGnanByParams(queryParams);
    }

    @Override
    public List<SysGncdQxanEntity> queryQxanByParams(Map<String,Object> queryParams) {
        return menuDao.queryQxanByParams(queryParams);
    }

    @Override
    public SysGncdQxanEntity queryQxanBylsh(String sjlsh) {
        return menuDao.queryQxanBylsh(sjlsh);
    }

    @Override
    public void updateGnan(SysGncdGnanEntity sgge) {
        menuDao.updateGnan(sgge);
    }

    @Override
    public void updateQxan(SysGncdQxanEntity sgge) {
        menuDao.updateQxan(sgge);
    }

    @Override
    public SysGncdEntity queryGncdInfo(String mcode) {
        SysGncdEntity sge = menuDao.queryGncdInfo(mcode);
        if (sge.getPcode() != null) {
            try {
                SysGncdEntity psge = menuDao.queryGncdInfo(sge.getPcode());
                if (psge != null) {
                    sge.setSjcd(psge.getMcaption());
                }
            } catch (Exception e) {
                log.error("psge空指,{}", e);
            }
        }
        return sge;
    }

    @Override
    public void delGncd(String mcode) {
        menuDao.delGncd(mcode);
    }

    @Override
    public void updateGncd(SysGncdEntity sge) {
        //根据pcode查询父级菜单级别，然后确定本菜单级别
        String pcode = sge.getPcode();
        SysGncdEntity psge = menuDao.queryGncdInfo(pcode);

        if (psge != null && psge.getCdjb() != null) {
            int pcdjb = psge.getCdjb();
            sge.setCdjb(pcdjb + 1);
        }
        String mcode=sge.getMcode();
        //查询修改前的父级菜单，判断是否为叶子节点并修改
        SysGncdEntity psgeBefore=menuDao.queryGncdInfo(mcode);
        //更新菜单
        menuDao.updateGncd(sge);

        menuDao.updateisYzjd(mcode);//更新修改后菜单叶子节点
        menuDao.updateisNotYzjd(mcode);
        menuDao.updateisYzjd(pcode);//更新修改后父级菜单的叶子节点
        menuDao.updateisNotYzjd(pcode);

        //根据查询修改前数据的父级菜单，若菜单的父级菜单修改，则更改修改前父级菜单的叶子节点
        if(psgeBefore!=null && !psgeBefore.getPcode().equals(pcode)){
            String  pcodeBefore=psgeBefore.getPcode();
            menuDao.updateisYzjd(pcodeBefore);//更新修改前父级菜单的叶子节点
            menuDao.updateisNotYzjd(pcodeBefore);
        }

    }

    @Override
    public void updateGncdList(SysGncdEntity sgge) {
        menuDao.updateGncd(sgge);
    }

    //根据菜单代码和功能代码查询
    @Override
    public SysGncdGnanEntity queryGnanByGndm(String cddm, String gndm) {
        return menuDao.queryGnanByGndm(cddm, gndm);
    }

}
